package com.yndsoft.jc.supplier.adapter;

import android.content.Context;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yndsoft.jc.R;
import com.yndsoft.jc.model.ReturnMsg;
import com.yndsoft.jc.supplier.hr.AddUpdateActivity;
import com.yndsoft.jc.supplier.model.EntryModel;
import com.yndsoft.jc.supplier.net.util.ProgressSubscriber;
import com.yndsoft.jc.supplier.utils.Constants;
import com.yndsoft.jc.supplier.utils.ToastUtil;
import com.yndsoft.jc.supplier.widget.recyclerview.RecyclerAdapter;
import com.yndsoft.jc.supplier.widget.recyclerview.RecyclerHolder;
import com.yndsoft.jc.supplier.widget.recyclerview.RecyclerListener;

import java.util.List;

public class EntryAdapter extends RecyclerAdapter<EntryModel> implements RecyclerListener.OnClickListener<RecyclerHolder> {

    private TextView hrTitle1;
    private TextView hrTitle2;
    private RelativeLayout hr3;
    private View hrLine;

    public EntryAdapter(Context context, List<EntryModel> results) {
        super(context, results, R.layout.item_hr);
    }

    @Override
    public void convert(RecyclerHolder holder, EntryModel model) {
        hrTitle1 = holder.findViewById(R.id.hr_title_1);
        hrTitle2 = holder.findViewById(R.id.hr_title_2);
        hr3 = holder.findViewById(R.id.hr_3);
        hrLine = holder.findViewById(R.id.hr_line);

        hrTitle1.setText("手机");
        hrTitle2.setText("昵称");
        hr3.setVisibility(View.GONE);
        hrLine.setVisibility(View.GONE);

        holder.setTvText(R.id.hr_value_1, model.getPhone());
        holder.setTvText(R.id.hr_value_2, model.getName());

        holder.setOnClickListener(this, R.id.hr_update, R.id.hr_delete);
    }

    @Override
    public void onClick(View v, final int position, RecyclerHolder holder) {
        switch (v.getId()) {
            case R.id.hr_update:
                EntryModel model = mData.get(position);
                AddUpdateActivity.start(mContext, Constants.ENTRY, Constants.UPDATE, model.getId(), model.getPhone(), model.getName(), model.getPwd());
                break;
            case R.id.hr_delete:
                mHttpUtil.hrDelete(new ProgressSubscriber(mContext) {
                    @Override
                    public void next(ReturnMsg returnMsg) {
                        if (1 == returnMsg.getCode()) {
                            ToastUtil.show(mContext, "删除成功！");
                            remove(position);
                        } else {
                            ToastUtil.show(mContext, "删除失败！");
                        }
                    }
                }, "deleteadmin", mData.get(position).getId(), mSession.getSid());
                break;
        }
    }
}
